package com.example.tpxz;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.ImageView;

/**
 * 图片的旋转
 */
public class MainActivity extends Activity {
    //记录角度
    private float jd = 0;
    private ImageView iv_img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_img = (ImageView) findViewById(R.id.iv_img);

        final Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.test);

        //子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    final Bitmap copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());

                    Canvas canvas = new Canvas(copyBitmap);

                    Paint paint = new Paint();

                    Matrix matrix = new Matrix();
                    //设置旋转的中心点和角度
                    matrix.setRotate(jd,srcBitmap.getWidth()/2,srcBitmap.getHeight()/2);

                    canvas.drawBitmap(srcBitmap,matrix,paint);


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            iv_img.setImageBitmap(copyBitmap);

                            jd += 10;

                            if(jd == 360){
                                jd = 0;
                            }
                        }
                    });


                    SystemClock.sleep(200);
                }

            }
        }).start();
    }
}
